package com.duoketech.controller.weixin;

import com.duoketech.config.Constant;
import com.duoketech.controller.BaseController;
import com.duoketech.model.activityorder.ActivityOrder;
import com.duoketech.model.member.Member;
import com.duoketech.model.order.Order;
import com.duoketech.model.roomorder.RoomOrder;
import com.duoketech.service.web.order.OrderService;
import com.duoketech.utils.pay.weixin.WxPayParamBuilder;
import com.duoketech.utils.pay.weixin.required.WxPayUtil;
import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.HashMap;
import java.util.Map;

public class WxPayController extends BaseController {
    public void index(){
    }

    public void doActivityPay(){
        try {
            int loginUserId = getMemberId();
            int id = getParaToInt("id",0);

            String nonceStr = WxPayUtil.generateNonceStr();
            String fee = getPara("fee","1");
            ActivityOrder orders = OrderService.ME.queryActivityOrderById(id);
            if(orders!=null){
                Member member = getMember();
                String openId =member.getStr(Member.OPEN_ID);
//                fee=XxxUtil.formatDecimal(orders.getBigDecimal(Orders.TOTALPAYMENTAMOUNT))+"";
                fee=orders.price();
                String orderId = orders.getStr(ActivityOrder.ORDER_SN);
                String s = WxPayParamBuilder.createWxPaySignWithNative("活动定金订单支付",nonceStr,id+"_ACT_"+orderId,fee,openId);
                Map<String,Object> result = WxPayParamBuilder.fetchNativeQrCode("活动定金订单支付",nonceStr,id+"_ACT_"+orderId,fee,s,loginUserId,id,0,openId);
                String prepayid = (String)result.get("prepay_id");
                Map<String, String> payParam = new HashMap<String, String>();
                payParam.put("appId", Constant.WX_APPID);
                payParam.put("nonceStr", WxPayUtil.generateNonceStr());
                payParam.put("package", "prepay_id="+prepayid);
                payParam.put("signType", "MD5");
                payParam.put("timeStamp", WxPayUtil.getCurrentTimestamp()+"");
                String secondSign = WxPayUtil.generateSignature(payParam, Constant.WX_PARTNER_KEY);
                payParam.put("paySign", secondSign);
                renderObject(payParam);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}
    public void doRoomPay(){
        try {
            int loginUserId = getMemberId();
            int id = getParaToInt("id",0);

            String nonceStr = WxPayUtil.generateNonceStr();
            String fee = getPara("fee","1");
            RoomOrder orders = OrderService.ME.queryRoomOrderById(id);
            if(orders!=null){
                Member member = getMember();
                String openId =member.getStr(Member.OPEN_ID);
//                fee=XxxUtil.formatDecimal(orders.getBigDecimal(Orders.TOTALPAYMENTAMOUNT))+"";
                fee=orders.totalPrice();
//                fee="0.01";
                String orderId = orders.getStr(RoomOrder.ORDER_SN);
                String s = WxPayParamBuilder.createWxPaySignWithNative("客栈房间订单支付",nonceStr,id+"_ROOM_"+orderId,fee,openId);
                Map<String,Object> result = WxPayParamBuilder.fetchNativeQrCode("客栈房间订单支付",nonceStr,id+"_ROOM_"+orderId,fee,s,loginUserId,id,0,openId);
                String prepayid = (String)result.get("prepay_id");
                Map<String, String> payParam = new HashMap<String, String>();
                payParam.put("appId", Constant.WX_APPID);
                payParam.put("nonceStr", WxPayUtil.generateNonceStr());
                payParam.put("package", "prepay_id="+prepayid);
                payParam.put("signType", "MD5");
                payParam.put("timeStamp", WxPayUtil.getCurrentTimestamp()+"");
                String secondSign = WxPayUtil.generateSignature(payParam, Constant.WX_PARTNER_KEY);
                payParam.put("paySign", secondSign);
                renderObject(payParam);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
}
    public void doProductPay() {
        try {
            int loginUserId = getMemberId();
            int id = getParaToInt("id", 0);

            String nonceStr = WxPayUtil.generateNonceStr();
            String fee = getPara("fee", "1");
            Order orders = OrderService.ME.queryProductOrderById(id);
            if (orders != null) {
                Member member = getMember();
                String openId = member.getStr(Member.OPEN_ID);
//                fee=XxxUtil.formatDecimal(orders.getBigDecimal(Orders.TOTALPAYMENTAMOUNT))+"";
                fee = orders.totalFeeWithDelivery();
//                fee="0.01";
                String orderId =orders.getStr(Order.ORDER_SER);
                String s = WxPayParamBuilder.createWxPaySignWithNative("商城商品订单支付", nonceStr, id + "_PRO_" + orderId, fee, openId);
                Map<String, Object> result = WxPayParamBuilder.fetchNativeQrCode("商城商品订单支付", nonceStr, id + "_PRO_" + orderId, fee, s, loginUserId, id, 0, openId);
                String prepayid = (String) result.get("prepay_id");
                Map<String, String> payParam = new HashMap<String, String>();
                payParam.put("appId", Constant.WX_APPID);
                payParam.put("nonceStr", WxPayUtil.generateNonceStr());
                payParam.put("package", "prepay_id=" + prepayid);
                payParam.put("signType", "MD5");
                payParam.put("timeStamp", WxPayUtil.getCurrentTimestamp() + "");
                String secondSign = WxPayUtil.generateSignature(payParam, Constant.WX_PARTNER_KEY);
                payParam.put("paySign", secondSign);
                renderObject(payParam);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Clear
    @Before(Tx.class)
    public void doPayCallBack(){
        String xml = HttpKit.readData(getRequest());
        String result = OrderService.ME.doPayCallBack(xml);
////        Member member = Member.dao.findById(32);
//        TemplateMsgHandler.getHandler().sendDepoistMsg(member,new BigDecimal(50));
        renderText(result);
    }
}
